<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;

class Basic extends Base
{

    public function choose_goods()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if (isset($data['goods_category']) && $data['goods_category'] != 0) {
                $list = Db::name('goods')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('goods_category_id', $data['goods_category'])->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('goods')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->where('goods_category_id', $data['goods_category'])->count();
            } else {
                $list = Db::name('goods')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('goods')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            foreach ($list as $key => $value) {
                $list[$key]['img_path'] = Db::name('upload_img')->where('id', $value['main_img_id'])->value('path');
            }
            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }
    public function choose_goods_one()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if (isset($data['goods_category']) && $data['goods_category'] != 0) {
                $list = Db::name('goods')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('goods_category_id', $data['goods_category'])->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('goods')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->where('goods_category_id', $data['goods_category'])->count();
            } else {
                $list = Db::name('goods')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('goods')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            foreach ($list as $key => $value) {
                $list[$key]['img_path'] = Db::name('upload_img')->where('id', $value['main_img_id'])->value('path');
            }
            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function one_goods()
    {
        $status = 0;
        $info = [];
        $data = $this->request->param();
        //halt($data);
        $goods['list'] = Db::name('goods')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('id', 'in', $data['ids'])->select();
        if (!empty($goods['list'])) {
            foreach ($goods['list'] as $key => &$val) {
                $val['goodstype'] = Db::name('goods_category')->where('id', $val['goods_category_id'])->value('name');
                $val['dw'] = Db::name('unit')->where('id', $val['unit'])->value('name');
                $val['unit_id'] = Db::name('unit')->where('id', $val['unit'])->value('id');
                $val['price'] = Db::name('goods_price_strategy')->where('goods_id', $val['id'])->find();
                $val['tax_rate'] = Db::name('system')->where('id', 1)->value('tax_rate');
            }

            //  $goods = Db::name('goods')->where('is_del', 0)->where('id', 'in', $data['ids'])->select();
            $goods['ids'] = implode(',', array_column($goods['list'], 'id'));
            $goods['names'] = implode(',', array_column($goods['list'], 'name'));
            $goods['count'] = count($goods['list']);
            $status = 1;
            $info = $goods;
        }
        return ['status' => $status, 'info' => $info];
    }

    // 客户-供应商     
    //single=1&is_customer=0,单选，供应商 
    //single=0&is_customer=0,多选，供应商
    //single=1&is_customer=1,单选，客户
    //single=0&is_customer=1,多选，客户 
    public function choose_customer()
    {
        $data = $this->request->param();
        //more == 0表示 多选 1表示单选
        if (!isset($data['single'])) {
            $data['single'] = 0; //多选
        }
        if (!isset($data['is_customer'])) {
            $data['is_customer'] = 1; //客户
        }
        $this->assign('single', $data['single']);
        $this->assign('is_customer', $data['is_customer']);
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            //0客户，1供应商 3两者
            $is_customer = $data['is_customer'] == 0 ? '1, 3' : '0, 3';


            if (isset($data['customer_category']) && $data['customer_category_category'] != 0) {
                $list = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('category_id', $data['customer_category'])->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->where('category_id', $data['customer_category'])->count();
            } else {
                $list = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }



    // 选择凭证
    public function choose_pz_imgs()
    {
        $data = $this->request->param();
        if (!isset($data['is_dj']) || $data['is_dj'] == 0) {
            $data['is_dj'] = 0;
            $dj = [0];
        }
        if ($data['is_dj'] == 1) {
            $dj = [0, 1];
        }

        if (!isset($data['order']) || $data['order'] == 0) {
            $order = 0;
        } else {
            $order = 1;
        }
        if ($order == 1) {
            $o = 'asc';
        } else {
            $o = 'desc';
        }
        $plist = Db::name('upload_img')->where('type', 1)->where('is_dj', 'in', $dj)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->distinct(true)->field('create_date')->order('create_date ' . $o)->select();
        $list = Db::name('upload_img')->where('type', 1)->where('is_dj', 'in', $dj)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->select();
        $this->assign('plist', $plist);
        $this->assign('list', $list);
        $this->assign('is_dj', $data['is_dj']);
        $this->assign('order', $order);
        $this->assign('imgs', $data['imgs']);
        return $this->fetch();
    }

    public function one_customer()
    {
        $data = $this->request->param();
        if (isset($data['more']) && $data['more'] == 1) {
            $customer['list'] = Db::name('customer')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('id', 'in', $data['ids'])->select();
            foreach ($customer['list'] as $key => &$value) {
                $value['user_arrears'] = user_arrears($value['id']); //总欠款.
            }

            $customer['ids'] = implode(',', array_column($customer['list'], 'id'));
            $customer['names'] = implode(',', array_column($customer['list'], 'name'));
        } else {
            if (is_array($data['id'])) {
                $customer['list'] = Db::name('customer')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('id', 'in', $data['id'])->select();
                foreach ($customer['list'] as $key => &$value) {
                    $value['user_arrears'] = user_arrears($value['id']); //总欠款.
                }

                $customer['ids'] = implode(',', array_column($customer['list'], 'id'));
                $customer['names'] = implode(',', array_column($customer['list'], 'name'));
            }else{
                $customer = Db::name('customer')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('id', intval($data['id']))->find();
                $customer['user_arrears'] = user_arrears($customer['id']); //总欠款

            }
            
        }

        if ($customer) {
            return json(['status' => 1, 'info' => $customer]);
        } else {
            return json(['status' => 0, 'info' => '查询失败']);
        }
    }



    // 供应商
    public function choose_supplier()
    {

        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (isset($data['customer_category']) && $data['customer_category_category'] != 0) {
                $list = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('category_id', $data['customer_category'])->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->where('category_id', $data['customer_category'])->count();
            } else {
                $list = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('customer')->where('is_customer', 'in', $is_customer)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function one_supplier()
    {

        $data = $this->request->param();
        $customer = Db::name('customer')->where('is_del', 0)->where('id', 'in', $data['ids'])->select();
        $customer['ids'] = implode(',', array_column($customer, 'id'));
        $customer['names'] = implode(',', array_column($customer, 'name'));
        if ($customer) {
            return json(['status' => 1, 'info' => $customer]);
        } else {
            return json(['status' => 0, 'info' => '查询失败']);
        }
    }


    // 仓库
    public function choose_store()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = Db::name('store')->where('is_del', 0)->where('status', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('store')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function one_store()
    {
        $data = $this->request->param();
        $store = Db::name('store')->where('is_del', 0)->where('id', 'in', $data['ids'])->select();
        $store['ids'] = implode(',', array_column($store, 'id'));
        $store['names'] = implode(',', array_column($store, 'name'));
        if ($store) {
            return json(['status' => 1, 'info' => $store]);
        } else {
            return json(['status' => 0, 'info' => '查询失败']);
        }
    }

    public function custom_field()
    {
        return $this->fetch();
    }
    //选择原单-销货单
    public function choose_source_list()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if ($map = '') {
                $list = Db::name('order')->where('o_type', 'in', [1, 2])->where('user_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> yh_price')->where('status', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('order')->where('o_type', 'in', [1, 2])->where('user_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> yh_price')->where('status', 1)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('order')->where('o_type', 'in', [1, 2])->where('user_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> yh_price')->where('status', 1)->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('order')->where('o_type', 'in', [1, 2])->where('user_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> yh_price')->where('status', 1)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            foreach ($list as $key => &$val) {
                $val['o_type_name'] = $val['o_type'] == 1 ? "普通销售" : "销售退回";
                $val['dj_time'] = date('Y-m-d', $val['dj_time']);
                $val['dj_jine'] = $val['o_type'] == 1 ? floatval($val['yh_price']) - floatval($val['payment']) : floatval($val['payment']) - floatval($val['yh_price']);
                $val['dj_jine'] = sprintf("%.2f", $val['dj_jine']);
                $val['already_jine'] = floatval('0.00');
                $val['not_jine'] = $val['dj_jine'] - $val['already_jine'];
            }
            return $this->grid_data($list, $count, $page, $limit);
        }
        $this->assign('user_id', $data['user_id']);
        return $this->fetch();
    }

    //选择原单-购货单
    public function choose_source_pay_list()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);


            if ($map = '') {
                $list = Db::name('invpu')->where('o_type', 'in', [1, 2])->where('supplier_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> after_discount')->where('check_status', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('invpu')->where('o_type', 'in', [1, 2])->where('supplier_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> after_discount')->where('check_status', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->count();
                // $count = Db::name('invpu')->where('supplier_id', $data['user_id'])->where('user_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> yh_price')->where('status', 1)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('invpu')->where('o_type', 'in', [1, 2])->where('supplier_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> after_discount')->where('check_status', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('invpu')->where('o_type', 'in', [1, 2])->where('supplier_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> after_discount')->where('check_status', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->count();
                // $list = Db::name('order')->where('o_type', 'in', [1, 2])->where('user_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> yh_price')->where('status', 1)->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                // $count = Db::name('order')->where('o_type', 'in', [1, 2])->where('user_id', $data['user_id'])->where('is_hexiao', 'neq', 1)->where('payment', 'exp', '<> yh_price')->where('status', 1)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            foreach ($list as $key => &$val) {
                $val['o_type_name'] = $val['o_type'] == 1 ? "普通采购" : "采购退回";
                $val['dj_time'] = date('Y-m-d', $val['dj_time']);
                $val['dj_jine'] = $val['o_type'] == 1 ? floatval($val['after_discount']) - floatval($val['payment']) : floatval($val['payment']) - floatval($val['after_discount']);
                $val['dj_jine'] = sprintf("%.2f", $val['dj_jine']);
                $val['already_jine'] = floatval('0.00');
                $val['not_jine'] = $val['dj_jine'] - $val['already_jine'];
            }
            return $this->grid_data($list, $count, $page, $limit);
        }
        $this->assign('user_id', $data['user_id']);
        return $this->fetch();
    }


    //选择原单-收款单
    public function choose_source_payment_list()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $map = '';
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $type = [0]; //收款

            if ($map = '') {
                $list = Db::name('payment')->where('type', 'in', $type)->where('user_id', $data['user_id'])->where('is_hx', 'neq', 1)->where('now_jine', 'neq', 0)->where('status', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('payment')->where('type', 'in', $type)->where('user_id', $data['user_id'])->where('is_hx', 'neq', 1)->where('now_jine', 'neq', 0)->where('status', 1)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('payment')->where('type', 'in', $type)->where('user_id', $data['user_id'])->where('is_hx', 'neq', 1)->where('now_jine', 'neq', 0)->where('status', 1)->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('payment')->where('type', 'in', $type)->where('user_id', $data['user_id'])->where('is_hx', 'neq', 1)->where('now_jine', 'neq', 0)->where('status', 1)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }
            foreach ($list as $key => &$val) {
                $val['o_type_name'] = $val['type'] == 0 ? "收款" : "其它";
                $val['dj_time'] = date('Y-m-d', $val['dj_time']);
                $val['dj_jine'] = sprintf("%.2f", floatval($val['jine'])); //
                $val['already_jine'] = sprintf("%.2f", floatval($val['hexiao_jine'])); //核销总金额
                $val['not_jine'] = sprintf("%.2f", floatval($val['now_jine'])); //本次预收款
            }
            return $this->grid_data($list, $count, $page, $limit);
        }
        $this->assign('user_id', $data['user_id']);
        return $this->fetch();
    }

    //单个收付款单
    public function one_payment()
    {
        $status = 0;
        $info = [];
        $data = $this->request->param();
        //halt($data);
        $order = Db::name('payment')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('id', 'in', $data['ids'])->select();
        foreach ($order as $key => &$val) {
            $val['o_type_name'] = $val['type'] == 0 ? "收款" : "付款";
            $val['dj_time'] = date('Y-m-d', $val['dj_time']);
            $val['dj_jine'] = sprintf("%.2f", floatval($val['jine']));
            $val['already_jine'] = sprintf("%.2f", floatval($val['hexiao_jine'])); //核销总金额
            $val['not_jine'] = sprintf("%.2f", floatval($val['now_jine'])); //本次预收款
        }
        if (!empty($order)) {
            $status = 1;
            $info = $order;
        }
        return ['status' => $status, 'info' => $info];
    }

    //获取单体-订单
    public function one_order()
    {
        $status = 0;
        $info = [];
        $data = $this->request->param();
        //halt($data);
        $order = Db::name('order')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->where('id', 'in', $data['id'])->select();
        foreach ($order as $key => &$val) {
            $val['o_type_name'] = $val['o_type'] == 1 ? "普通销售" : "销售退回";
            $val['dj_time'] = date('Y-m-d', $val['dj_time']);
            $val['dj_jine'] = $val['o_type'] == 1 ? floatval($val['yh_price']) - floatval($val['payment']) : floatval($val['payment']) - floatval($val['yh_price']);
            $val['already_jine'] = floatval('0.00');
            $val['not_jine'] = $val['dj_jine'] - $val['already_jine'];
        }
        if (!empty($order)) {
            $status = 1;
            $info = $order;
        }
        return ['status' => $status, 'info' => $info];
    }

    // 选择模板
    public function choose_template()
    {

        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (!isset($data['type'])) {
                $data['type'] = 1; // 拆卸单
            } else {
                $data['type'] = 0; // 组装单
            }

            $list = Db::name('template')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('template')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();

            foreach ($list as $key => $value) {
                $goods = Db::name('goods')->where('id', $value['goods_id'])->find();
                $list[$key]['g_name'] = $goods['sn'] . ' ' . $goods['name'] . ' ' . $goods['model'];
                $details = Db::name('template_details')->where('pid', $value['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
                foreach ($details as $k => $v) {
                    $details_goods = Db::name('goods')->where('id', $v['goods_id'])->find();
                    $list[$key]['son'][$k]['g_name'] = $details_goods['sn'] . ' ' . $details_goods['name'] . ' ' . $details_goods['model'];
                    $list[$key]['son'][$k]['number'] = $v['number'];
                }
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function one_template()
    {
        $data = $this->request->param();
        $customer = Db::name('customer')->where('is_del', 0)->where('id', 'in', $data['ids'])->select();
        $customer['ids'] = implode(',', array_column($customer, 'id'));
        $customer['names'] = implode(',', array_column($customer, 'name'));
        if ($customer) {
            return json(['status' => 1, 'info' => $customer]);
        } else {
            return json(['status' => 0, 'info' => '查询失败']);
        }
    }

    // 多账户结算
    public function multiple_accounts()
    {
        return $this->fetch();
    }
    // 选择职员（销售人员，收款人）
    public function choose_staff()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = Db::name('auth_user')->where('type', 1)->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('auth_user')->where('type', 1)->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }
    //单个职员
    public function one_staff()
    {
        $data = $this->request->param();
        $staff = Db::name('auth_user')->where('type', 1)->where('is_del', 0)->where('id', 'in', $data['ids'])->select();
        $staff['ids'] = implode(',', array_column($staff, 'id'));
        $staff['names'] = implode(',', array_column($staff, 'name'));
        if ($staff) {
            return json(['status' => 1, 'info' => $staff]);
        } else {
            return json(['status' => 0, 'info' => '查询失败']);
        }
    }

    /**
     * @desc 根据基本单位查询基本单位和副单位
     * @param $pid 多单位的基本单位
     */
    public function list_unit()
    {
        $data = $this->request->param();
        $list = Db::name('unit')->where('pid', $data['pid'])->whereOr('id', $data['pid'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
        if ($list) {
            return json(['status' => 1, 'msg' => '查询成功', 'list' => $list]);
        } else {
            return json(['status' => 0, 'msg' => '查询失败']);
        }
    }

    // 上传图片
    public function upload_img()
    {
        $post = $this->request->param();
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        if (isset($post['path'])) {
            $path = $post['path'];
        } else {
            $path = "good";
        }
        if (isset($post['type'])) {
            $imgType = $post['type'];
        } else {
            $imgType = 0;
        }
        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->validate(['ext' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $path);
            if ($info) {
                // 成功上传后 获取上传信息
                // 输出 jpg
                $data['extension'] = $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $data['path'] = $path . '/' . $info->getSaveName();
                $data['addtime'] = time();
                $data['updatetime'] = time();
                $data['create_date'] = date('Y-m-d', time());
                $data['enterprise_id'] = $this->EnterpriseId;
                $data['type'] = $imgType;
                $res = Db::name('upload_img')->insertGetId($data);
                if ($res) {
                    $data['id'] = $res;
                    return json(['status' => 1, 'msg' => '上传成功', 'info' => $data]);
                } else {
                    return json(['status' => 0, 'msg' => '上传失败']);
                }
            } else {
                // 上传失败获取错误信息
                $error = $file->getError();
                return json(['status' => 0, 'msg' => '上传失败' . $error]);
            }
        }
    }

    /**
     * base64 上传图片（多图）
     * @param string $base64_img 图片base64编码
     * @param string $path 根目录
     * @param string $savepath 保存位置
     * @return array
     * */
    function baseimgs_upload()
    {

        Db::startTrans(); // 启动事务
        $status = 0;
        $msg = "上传失败";
        $img = '';
        //1.获取一下提交的数据,包括上传文件
        $post = $this->request->param(true);
        //  halt($data);
        if (isset($post['path'])) {
            $path = $post['path'];
        } else {
            $path = "good";
        }
        if (isset($post['type'])) {
            $imgType = $post['type'];
        } else {
            $imgType = 0;
        }
        $imgArr = [];
        $arr = explode(',', $post['image']);
        $num = count($arr) / 2;
        $base_arr = [];
        for ($i = 0; $i < $num; $i++) {
            array_push($base_arr, $arr[$i * 2] . ',' . $arr[$i * 2 + 1]);
        }

        $flag = 0;
        $img_data = []; //插入数据库的数组
        for ($i = 0; $i < count($base_arr); $i++) {
            $base64_img = $base_arr[$i];
            $base64_img = str_replace('', '+', $base64_img);
            $up_dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $path . DS . date('Ym');
            if (!file_exists($up_dir)) {
                mkdir($up_dir, 0777, true);
            }
            $base64_img = $base64_img;
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)) {
                //halt($result);
                $type = $result[2];
                if (in_array($type, array('pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png'))) {
                    $randStr = str_shuffle('1234567890'); //随机字符串
                    $rand = substr($randStr, 0, 4); // 返回前四位
                    $new_file = $up_dir . '/' . date('YmdHis') . $rand . '.' . $type;  //完整路径 + 图片名
                    $img = $path . '/' . date('Ym') . '/' . date('YmdHis') . $rand . '.' . $type; //图片名
                    if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))) {
                        array_push($imgArr, $img);
                        // 输出 jpg
                        $data[$i]['extension'] =  $type;
                        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                        $data[$i]['path'] =  $img;
                        $data[$i]['addtime'] =  time();
                        $data[$i]['updatetime'] =  time();
                        $data[$i]['create_date'] = date('Y-m-d', time());
                        $data[$i]['enterprise_id'] = $this->EnterpriseId;
                        $data[$i]['type'] = $imgType;
                        array_push($img_data, $data[$i]);
                        $flag += 1;
                    } else {
                        $flag += 0;
                    }
                }
            }
        }
        if ($flag == count($base_arr)) {
            //halt($img_data);
            $res = Db::name('upload_img')->insertAll($img_data);

            if ($res) {
                Db::commit(); // 提交事务
                return json(['status' => 1, 'msg' => "图片上传成功", 'img' => $imgArr]);
            }
        }
        Db::rollback(); // 回滚事务
        return json(['status' => 0, 'msg' => "图片上传失败"]);
    }



    // 选择结算账户
    public function choose_account()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            $list = Db::name('account')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
            $count = Db::name('account')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            foreach ($list as $key => $value) {
                //0现金，1银行存款',
                $list[$key]['type'] = $value['type'] == 0 ? '现金' : '银行存款';
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }
    //单个结算账户
    public function one_account()
    {
        $data = $this->request->param();
        $account = Db::name('account')->where('is_del', 0)->where('id', 'in', $data['ids'])->select();
        $account['ids'] = implode(',', array_column($account, 'id'));
        $account['names'] = implode(',', array_column($account, 'name'));
        if ($account) {
            return json(['status' => 1, 'info' => $account]);
        } else {
            return json(['status' => 0, 'info' => '查询失败']);
        }
    }

    // 查询库存
    public function get_stock($store_id, $good_id)
    {
        $data = $this->request->param();
   
        // $num = get_stock($data['store_id'], $data['goods_id']);
        $num = get_stock($store_id, $good_id);
        return json(['status' => 1, 'msg' => '查询成功', 'info' => $num]);
    }

    // 单个模板
    public function one_templates()
    {
        $data = $this->request->param();
        $list = Db::name('template')->where('id', $data['id'])->find();
        $list['goods_name'] = Db::name('goods')->where('id', $list['goods_id'])->value('name');
        $list['unit_name'] = Db::name('unit')->where('id', $list['unit_id'])->value('name');
        $list['store_name'] = Db::name('store')->where('id', $list['store_id'])->value('name');

        $details = Db::name('template_details')->where('pid', $list['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->select();
        foreach ($details as $key => $value) {
            $details[$key]['goods_name'] = Db::name('goods')->where('id', $value['goods_id'])->value('name');
            $details[$key]['unit_name'] = Db::name('unit')->where('id', $value['unit_id'])->value('name');
            $details[$key]['store_name'] = Db::name('store')->where('id', $value['store_id'])->value('name');
        }
        return json(['status' => 1, 'msg' => '查询成功', 'list' => $list, 'details' => $details]);
    }

    // 获取客户及联系人信息
    public function get_customer_info()
    {
        $data = $this->request->param();
        $info = Db::name('customer')->where('id', $data['id'])->find();
        $info['ext'] = Db::name('customer_ext')->where('customer_id', $info['id'])->where(['is_del' => 0, 'enterprise_id' => $this->EnterpriseId])->find();
        return json(['status' => 1, 'msg' => '查询成功', 'info' => $info]);
    }


    // 编辑联系地址
    public function edit_address()
    {
        $data = $this->request->param();
        return $this->fetch();
    }

     // 高级搜索
     public function hight_search()
     {
         $data = $this->request->param();
         $this->assign('o_type',$data['o_type']);//订单类别：0,销售订单，1，销售单，2，销售退货单，3,购货订单，4，购货单，5，购货退货单
         //制单人 
         $add_user = db('auth_user')->where('is_del',0)->where('enterprise_id' , $this->EnterpriseId)->select();
         //审核人
         $shenhe_user = db('auth_user')->where('is_del',0)->where('enterprise_id' , $this->EnterpriseId)->select();
         //最后修改人
         $last_user = db('auth_user')->where('is_del',0)->where('enterprise_id' , $this->EnterpriseId)->select();
         //收款账户
         $recAccName = db('account')->where('is_del',0)->where('enterprise_id' , $this->EnterpriseId)->select();
         //付款账户
         $payAccName = db('account')->where('is_del',0)->where('enterprise_id' , $this->EnterpriseId)->select();
         //仓库
         $store = db('store')->where('is_del',0)->where('enterprise_id' , $this->EnterpriseId)->select();
         $this->assign('store',$store);
         $this->assign('payAccName',$payAccName);
         $this->assign('recAccName',$recAccName);
         $this->assign('last_user',$last_user);
         $this->assign('shenhe_user',$shenhe_user);
         $this->assign('add_user',$add_user);
         return $this->fetch();
     }

    //  导入
    public function import()
    {
        $data = $this->request->param();
        return $this->fetch();
    }

    //  批量导入
    public function batch_import()
    {
        $data = $this->request->param();
        return $this->fetch();
    }
}
